/**
 * Copyright &copy; 2015-2020 <a href="www.dsesa.cn/">ESA</a> All rights reserved.
 */
package com.jeeplus.modules.actSetting.web;

import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.web.BaseController;
import com.jeeplus.modules.actSetting.entity.ActSetting;
import com.jeeplus.modules.actSetting.mapper.ActSettingMapper;
import com.jeeplus.modules.actSetting.service.ActSettingService;
import com.jeeplus.modules.sys.entity.ArchCostItem;
import com.jeeplus.modules.sys.entity.DictValue;
import com.jeeplus.modules.sys.entity.Role;
import com.jeeplus.modules.sys.service.SystemService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xujin
 * @version 2019-10-08
 */
@Controller
@RequestMapping(value = "${adminPath}/actSetting")
public class ActSettingController extends BaseController {

	@Autowired
	private ActSettingService actSettingService;

	@Autowired
	private ActSettingMapper actSettingMapper;


	@Autowired
	private SystemService systemService;

	@RequestMapping(value = {"list", ""})
	public String list() {
		return "modules/actSetting/actSettingList";
	}

	@ResponseBody
	@RequestMapping(value = "actData")
	public Map<String, Object> actData() {
		Map<String, Object> map = new HashMap<String, Object>();
		List<Map> list = actSettingMapper.getList();
		map.put("rows",list);
		map.put("total", list.size());
		return map;
	}
	@ResponseBody
	@RequestMapping(value = "getActSetting")
	public Map<String, Object> getActSetting(@RequestParam(value = "objectId") String objectId) {
		Map<String, Object> map = new HashMap<String, Object>();
		if(objectId == null || "".equals(objectId)){
			map.put("rows","[]");
			map.put("total",0);
		}else{
			List<ActSetting> list = actSettingService.getRoleList(objectId);
			map.put("rows",list);
			map.put("total", list.size());
		}
		return map;
	}

	@ResponseBody
	@RequestMapping(value = "data")
	public Map<String, Object> data(ActSetting actSetting, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<ActSetting> page = actSettingService.findPage(new Page<ActSetting>(request, response), actSetting);
		return getBootstrapData(page);
	}

	@ResponseBody
	@RequestMapping(value = "roleList")
	public Map<String, Object> roleList(String objectId, HttpServletRequest request, HttpServletResponse response, Model model) {
		List<Role> list = systemService.findAllRole();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("rows", list);
		map.put("total", list.size());
		return map;
	}

	@RequestMapping(value = "roleSelect")
	public String roleSelect(boolean isMultiSelect, String objectId, Model model) {
		model.addAttribute("isMultiSelect", isMultiSelect);
		model.addAttribute("objectId", objectId);
		return "modules/actSetting/roleSelect";
	}

	/**
	 * 款项分配
	 */
	@ResponseBody
	@RequestMapping(value = "assignRole")
	public AjaxJson assignRole(String procDefId, String procDefkey, String roleId) {
		AjaxJson j = new AjaxJson();
		int newNum = 0;
		String [] roleIdArr = roleId.split(",");
		for(int i = 0; i<roleIdArr.length; i++){
			if(actSettingService.setRole(procDefId,procDefkey,roleIdArr[i])){
				newNum++;
			}
		}

		if(newNum>0){
			j.setSuccess(true);
			j.setMsg("已成功分配 "+newNum+" 个角色");
		}else {
			j.setSuccess(false);
			j.setMsg("添加失败");
		}
		return j;
	}

	@ResponseBody
	@RequestMapping(value = "deleteRole")
	public AjaxJson deleteRole(String id) {
		AjaxJson j = new AjaxJson();
		actSettingService.delete(id);
		j.setSuccess(true);
		j.setMsg("删除键值成功！");
		return j;
	}

	@ResponseBody
	@RequestMapping(value = "auth")
	public AjaxJson auth(String id) {
		AjaxJson j = new AjaxJson();
		actSettingService.auth();
		j.setSuccess(true);
		j.setMsg("更新成功！");
		return j;
	}

}
